package com.lvy.hczn.front.business.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.MqttRecord;
import com.lvy.hczn.front.business.mapper.MqttRecordMapper;
import com.lvy.hczn.front.business.service.MqttRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MqttRecordServiceImpl extends ServiceImpl<MqttRecordMapper, MqttRecord> implements MqttRecordService {

    @Autowired
    private MqttRecordMapper mqttRecordMapper;

    /**
     * 记录通讯信息
     *
     * @param boxCode
     * @param topic
     * @param state
     * @param jsonStr
     * @return void
     * @author yw
     * @date 2022-08-06 19:44:39
     */
    @Override
    public void initMqttRecord(String boxCode, String topic, Integer state, String jsonStr) {
        ThreadUtil.execAsync(() -> {
            MqttRecord mqttRecord = new MqttRecord();
            mqttRecord.setBoxCode(boxCode);
            mqttRecord.setTopic(topic);
            mqttRecord.setState(state);
            mqttRecord.setDone(true);
            mqttRecord.setContent(jsonStr);
            mqttRecordMapper.insert(mqttRecord);
        });
    }

    /**
     * @Description 物理删除
     * @Param
     * @Author yw
     * @Date 2024/9/12 10:31
     * @Return
     **/
    @Override
    public int logicDelete(MqttRecord mqttRecord) {
        if (mqttRecord.getCreateTime() == null || mqttRecord.getUpdateTime() == null) {
            throw new UtilException("参数为空");
        }
        return mqttRecordMapper.logicDataDelete(mqttRecord);
    }
}
